package com.health.central_authentication.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.health.result.CodeStatus;
import com.health.result.ResultUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限不足处理器
 * @author: zipeng Li
 * 2021/6/13  13:01
 */
@Component
public class DGUTAccessDeniedHandler implements AccessDeniedHandler {
    @Resource
    ObjectMapper mapper;

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("text/javascript;charset=utf-8");
        httpServletResponse.getWriter().print(mapper.writeValueAsString(ResultUtils.error("没有访问权限!", CodeStatus.NO_AUTH)));
    }
}
